---
- name: ensure packages required for libtaskotron are installed (yum)
  package: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - PyYAML
    - libtaskotron
  when: deployment_type == 'local'

- name: ensure packages required for libtaskotron are installed (dnf)
  dnf: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - PyYAML
    - libtaskotron
    - testcloud
    - selinux-policy-devel
  when: deployment_type in ['dev', 'stg', 'prod']

- name: ensure packages required for taskotron tasks are installed (yum)
  package: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - python-solv
    - python-librepo
    - rpmlint
  when: ansible_distribution_major_version|int < 22

- name: ensure packages required for taskotron tasks are installed (dnf)
  dnf: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - python-solv
    - python-librepo
    - rpmlint
  when: ansible_distribution_major_version|int > 21 and ansible_cmdline.ostree is not defined

- name: generate taskotron.yaml config file
  template: src={{ item }} dest=/etc/taskotron/taskotron.yaml owner=root group=root mode=0644
  with_first_found:
    - taskotron.yaml.j2.{{ deployment_type }}
    - taskotron.yaml.j2

- name: generate namespaces.yaml config file
  template: src={{ item }} dest=/etc/taskotron/namespaces.yaml owner=root group=root mode=0644
  with_first_found:
    - namespaces.yaml.j2.{{ deployment_type }}
    - namespaces.yaml.j2

- name: generate testcloud config file
  template: src={{ item }} dest=/etc/testcloud/settings.py owner=root group=root mode=0644
  with_first_found:
    - settings.py.testcloud.j2.{{ deployment_type }}
    - settings.py.testcloud.j2

- name: upload yumrepoinfo.conf
  copy: src={{ item }} dest=/etc/taskotron/yumrepoinfo.conf owner=root group=root mode=0644
  with_first_found:
    - yumrepoinfo.conf.{{ deployment_type }}
    - yumrepoinfo.conf

- name: check that baseurl= line exists in yumrepoinfo.conf
  command: grep -q '^baseurl =' /etc/taskotron/yumrepoinfo.conf
  changed_when: False

- name: check that baseurl_altarch= line exists in yumrepoinfo.conf
  command: grep -q '^baseurl_altarch =' /etc/taskotron/yumrepoinfo.conf
  changed_when: False

- name: set infra repo for baseurl in yumrepoinfo.conf
  lineinfile:
    path: /etc/taskotron/yumrepoinfo.conf
    regexp: '^baseurl ='
    line: 'baseurl = http://infrastructure.fedoraproject.org/pub/fedora/linux'

- name: set infra repo for baseurl_altarch in yumrepoinfo.conf
  lineinfile:
    path: /etc/taskotron/yumrepoinfo.conf
    regexp: '^baseurl_altarch ='
    line: 'baseurl_altarch = http://infrastructure.fedoraproject.org/pub/fedora-secondary'

- name: make sure there's no download.fp.o leftover in yumrepoinfo.conf
  command: grep -qE '^[^#].*/download.fedoraproject.org/' /etc/taskotron/yumrepoinfo.conf
  changed_when: False
  register: result
  failed_when: result.rc != 1

- name: create /var/log/taskotron for task logfiles
  file: path=/var/log/taskotron state=directory owner={{ slave_user }} group={{ slave_user }} mode=1755
  when: deployment_type == 'local'

- name: ensure buildslaves group exists
  group: name={{ slaves_group }}
  when: deployment_type in ['dev', 'stg', 'prod']

- name: create directory for disposable clients images
  file: path={{ imagesdir }} state=directory owner=root group={{ slaves_group }} mode=1755
  when: deployment_type in ['dev', 'stg', 'prod']

- name: create /var/log/taskotron for task logfiles
  file: path=/var/log/taskotron state=directory owner=root group={{ slaves_group }} mode=1775
  when: deployment_type in ['dev', 'stg', 'prod']

- name: upload custom selinux policy to allow buildslaves to use qemu-kvm
  copy: src=service-virt-transition.te dest=/root/service-virt-transition.te owner=root group=root mode=0644
  when: deployment_type in ['dev', 'stg', 'prod']

- name: compile selinux policy
  command: chdir=/root/ creates=/root/service-virt-transition.pp make -f /usr/share/selinux/devel/Makefile
  when: deployment_type in ['dev', 'stg', 'prod']

- name: load custom selinux policy for qemu-kvm from buildslaves
  command: chdir=/root/ semodule -i /root/service-virt-transition.pp
  when: deployment_type in ['dev', 'stg', 'prod']

- name: enable libvirtd
  service: name=libvirtd state=started enabled=yes
  when: ansible_distribution_major_version|int < 24

- name: start and enable libvirt services
  service: name={{ item }} state=started enabled=yes
  with_items:
    - libvirtd
    - virtlogd
  when: ansible_distribution_major_version|int >= 24

- name: copy custom libvirt network config
  copy: src=default.xml dest=/etc/libvirt/qemu/networks/default.xml owner=root group=root mode=0600
  when: deployment_type in ['dev', 'stg', 'prod']

- name: hotfix libtaskotron to set longer timeout for testcloud, this should be temporary fix though :/
  replace: dest=/usr/lib/python2.7/site-packages/libtaskotron/ext/disposable/vm.py regexp=', timeout=60\):' replace=', timeout=120):' backup=yes
  when: deployment_type in ['dev', 'stg', 'prod']

- name: setup nested virt on virthosts with nested=true variable
  copy: src=kvm_intel.conf dest=/etc/modprobe.d/kvm_intel.conf
  when: nested == true

- name: copy domain xml for testcloud that allows nested virt
  copy: src=domain-template.jinja dest=/var/lib/testcloud/domain-template.jinja owner=qemu group=testcloud mode=0764
  when: deployment_type in ['dev', 'stg', 'prod']
